import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;




public class MapHandler {

	//Tiles that the player can walk on
	private static final int EMPTYTILE    		    = 0;
	private static final int GOALTILE     		    = 1;
	private static final int CORNERTILE   		    = 2;
	private static final int ADJTOWALLTILE_NS       = 3;
	private static final int ADJTOWALLTILE_EW  		= 4;
	private static final int DEATHTILE		  		= 5;
	
	//Tiles that the player cannot walk on
	private static final int WALLTILE     		 	= 6;
	
	


	StaticMap staticMap;

	private int boardW;

	private int boardH;
	public static  Socket socket;
	public static InputStream inRaw;
	public static BufferedReader in;
	public static PrintWriter out;
	public static String arg;
	
 	public static Socket getSocket(){
	  return socket;
  	}
  	public static BufferedReader getBufferedReader(){
	  return in;
  	}
  
  	public static PrintWriter getPrintWriter(){
	  return out;
  	}
	
	public MapHandler(int boardNumber, BoxList boxList, Coord playerPos) {
		staticMap = StaticMap.getStaticMap();

		byte[] boardbytes = new byte[1024];
        String asciiBoard;

        try {
            socket = new Socket("cvap103.nada.kth.se",5555);
            inRaw = socket.getInputStream();
            in = new BufferedReader(new InputStreamReader(inRaw));
            out = new PrintWriter(socket.getOutputStream(), true);
            out.println(String.valueOf(boardNumber));
            inRaw.read(boardbytes);
            asciiBoard = new String(boardbytes);
            //System.out.println(asciiBoard);     //Prints an ASCII representation of the board 
            int size=asciiBoard.length();
            int height=0,width=0,start=0;
            
            while(start<size) {        
	            if(asciiBoard.charAt(start)==0X0A)
	            	height++;	  
	            start++;
	            if(height<1)
	                width++;
            }
            boardW=width;
            boardH=height-1;
            //System.out.println(boardW);
            //System.out.println(boardH);
           start=0;
           staticMap.initBoard(boardW,boardH);
           
           int x=0,y=0;
           while(start<boardW*boardH+boardH)
           {
        	   if (((start+1)%(boardW+1)==0) && (start !=0)) {
        		   y+=1;
        	       x=0;
        	   } else {
        		   
	            	switch(asciiBoard.charAt(start))
	                {
	            	  case 0x23:
	            		  staticMap.setTile(x, y, WALLTILE);       //Wall
	            		  break;
	            	  case 0x40:
	            		  playerPos.setValue(x, y);
	            		  staticMap.setTile(x, y, EMPTYTILE);
	            		  //staticMap.setTile(i, j, PLAYERTILE);   //Player
	            		  break;
	            	  case 0x2b:
	            		  playerPos.setValue(x, y);
	            		  staticMap.setTile(x, y, GOALTILE);       //Player on goal square  (PLAYERONGOALTILE)
	            		  break;
	            	  case 0x24:
	            		  //staticMap.setTile(x, y, BOXTILE);      //Box
	            		  staticMap.setTile(x, y, EMPTYTILE);
	            		  boxList.add(new Box(new Coord(x,y)));
	            		  break;
	            	  case 0x2a:
	            		  staticMap.setTile(x, y, GOALTILE);	   //Box on goal square     (BOXONGOALTILE)
	            		  boxList.add(new Box(new Coord(x,y)));        
	            		  break;
	            	  case 0x2e:
	            		  staticMap.setTile(x, y, GOALTILE);       //Goal tile
	            		  break;
	            	  case 0x20:
	            		  staticMap.setTile(x, y, EMPTYTILE);      //Floor 
	            		  break;
	            	  default: staticMap.setTile(x, y, EMPTYTILE); break;
	            		  
	                }
            	x+=1;
              }	
        	 start+=1;  
           } 

           /*
           
           for(i=0;i<boardH;i++)
           {
        	  for (j=0;j<boardW;j++)
        		  System.out.println(tileMatrix[i][j]); 
           }*/
           
           //out.close();
           //in.close();
           //socket.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }	
        /*
        playerPos.setValue(6, 3);
        boxList.add(new Box(new Coord(12,3)));
        
        for (int i=0 ; i<boardW ; i++) {
     	   for (int j=0 ; j<boardH ; j++) {
     		   staticMap.setTile(i, j, EMPTYTILE);
     	   }
        }
        for (int i=0 ; i<boardW ; i++) {
     	   staticMap.setTile(i, 0, WALLTILE);
     	   staticMap.setTile(i, boardH-1, WALLTILE);
        }
        for (int i=0 ; i<boardH ; i++) {
     	   staticMap.setTile(0, i, WALLTILE);
     	   staticMap.setTile(boardW-1, i, WALLTILE);
        }
        for (int i=0 ; i<4 ; i++) {
     	   for (int j=0 ; j<boardH ; j++) {
     		   if (j!=boardW/2)
     		   staticMap.setTile(i, j, WALLTILE);
     	   }
        }
        for (int i=1 ; i<5 ; i++) {
     	   staticMap.setTile(i, boardW/2, EMPTYTILE);
        }
        staticMap.setTile(1, boardW/2, GOALTILE);*/
	}
	
public void sendSolution(StringBuffer solution) {
	String result;
    try {
    	socket=getSocket();
        in = getBufferedReader();
        out = getPrintWriter();
        String solutionServer=solution.toString();
        //String solution = new String("0 3 3 1 0 0 2 1 2 2 0 2 2 1 3 3 3 3 2 1 1 3 0 3 0 1 2 2 0 3")
        out.println(solutionServer);
        result = in.readLine();
        System.out.println(result);
        out.close();
        in.close();
        socket.close();
        } 
    catch (Exception e) {
        throw new RuntimeException(e);
    }
	
		
	}
}
